package com.gsm.config;

import com.gsm.entity.SysUser;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 实现接口 HandlerInterceptor
 * preHandle()
 * postHandle()
 * afterCompletion()
 */
@Component
public class GlobalInterceptor implements HandlerInterceptor {

    /**
     * @param request
     * @param response
     * @param handler
     * @return 返回 true
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //改变同源策略，允许任意源请求
        /*response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        //response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
        response.setHeader("Access-Control-Max-Age", "86400");
        response.setHeader("Access-Control-Allow-Credentials","true");*/

        Map<String, String[]> map = request.getParameterMap();
        String[] infos = map.get("userInfo");
        if (infos == null) {
            return true;
        }
        SysUser sysUser = new SysUser();
        for (String i : infos) {
            String[] split = i.split("-");
            String k = split[0];
            String v = split[1];
            if ("jti".equals(k)) {
                sysUser.setUserId(Long.valueOf(v));
            } else if ("sub".equals(k)) {
                sysUser.setUserName(v);
            } else if ("iat".equals(k) || "exp".equals(k)) {

            }
        }
        request.setAttribute("user", sysUser);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
